using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Systems.CMS.TemplateGallery;
using Deepcode.Flux.Core.Systems.Workflow.Bases;

public partial class Private_CMS_main_actions_submit_chooseDefTaskpad : Deepcode.Flux.Core.UI.CMS.CMSActionPane
{
	#region Extra Viewstates
	/// <summary>
	/// Get/Set the CSV list of ID's
	/// </summary>
	protected string MultiIDs
	{
		get
		{
			object o = ViewState["MIDS"];
			if (o == null) return "";
			return (string)o;
		}
		set
		{
			ViewState["MIDS"] = value;
		}
	}
	/// <summary>
	/// Get the MutliIDs CSV as an array of ints
	/// </summary>
	protected int[] iMultiIDs
	{
		get
		{
			List<int> list = new List<int>();
			string mids = MultiIDs;
			if (mids == "") return list.ToArray();

			string[] items = mids.Split(',');
			foreach (string si in items)
			{
				try
				{
					int i = Int32.Parse(si);
					list.Add(i);
				}
				catch { }
			}
			return list.ToArray();
		}
	}
	#endregion
	#region Page Load Event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			MultiIDs = GetAttribute("MultiIDs", "");
			
			Definition [] available = new Definitions().GetDefinitionsByHandlerID("ContentManagement");
			if (available.Length == 0)
			{
				// There are no workflows - publish immediately
				PublishNow();
				GoBack();
			}
			else if (available.Length == 1)
			{
				// There is only one - publish using that
				PublishNow( available[0].PK_ID );
				GoBack();
			}
			else
			{
				fWorkflows.Items.Clear();
				foreach (Definition d in available)
					fWorkflows.Items.Add(new ListItem(d.DefName, d.PK_ID.ToString()));
			}
		}
	}
	#endregion
	#region Interactions
	/// <summary>
	/// Type selected, go forward to the actual editor form itself
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnNext_Click(object sender, EventArgs e)
	{
		if( ! Page.IsValid ) return;
	
		try
		{
			int selectedDef = Int32.Parse(fWorkflows.SelectedValue);
			PublishNow(selectedDef);
		}
		catch { }

		// Return back to list
		GoBack();
	}
	/// <summary>
	/// Cancel clicked - go back to content view listing
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		GoBack();
	}
	#endregion
	#region Publishing Function
	/// <summary>
	/// Publish without any workflow
	/// </summary>
	public void PublishNow()
	{
		ContentManager cms = new ContentManager();
		if (MultiIDs != "")
		{
			foreach (int i in iMultiIDs)
			{
				ContentObject o = cms.AdxGetByID( i );
				if (o != null)
				{
					cms.SubmitForApproval(i, -1, CurrentUser.PK_ID);

					Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
						"Flux",
						"Content Manager",
						"submitted for approval - {0}", o.NodeTitle);
				}
			}
		}
		else
		{
			ContentObject o = cms.AdxGetByID( ContentID );
			if (o != null)
			{
				cms.SubmitForApproval(ContentID, -1, CurrentUser.PK_ID);

				Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
					"Flux",
					"Content Manager",
					"submitted for approval - {0}", o.NodeTitle);

			}
		}
	}
	/// <summary>
	/// Publish using workflow definition specified
	/// </summary>
	/// <param name="DefID"></param>
	public void PublishNow(int DefID)
	{
		ContentManager cms = new ContentManager();
		if (MultiIDs != "")
		{
			foreach (int i in iMultiIDs)
			{
				ContentObject o = cms.AdxGetByID( i );
				if (o != null)
				{
					cms.SubmitForApproval(i, DefID, CurrentUser.PK_ID);
					Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
						"Flux",
						"Content Manager",
						"submitted for approval - {0}", o.NodeTitle);
				}
			}
		}
		else
		{
			ContentObject o = cms.AdxGetByID( ContentID );
			if (o != null)
			{
				cms.SubmitForApproval(ContentID, DefID, CurrentUser.PK_ID);
				Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
					"Flux",
					"Content Manager",
					"submitted for approval - {0}", o.NodeTitle);
			}
		}
	}
	#endregion
	#region Utility
	/// <summary>
	/// Override go back to return to taskpad
	/// </summary>
	protected override void GoBack()
	{
		Response.Redirect("~/admin/menu/taskpad.aspx", true);
	}
	#endregion
}
